//
//  GoddsEvaluateVC.m
//  CenturyCity
//
//  Created by beisheng03 on 2017/12/30.
//  Copyright © 2017年 beisheng. All rights reserved.
//

#import "GoddsEvaluateVC.h"

#import "EvalusteNothingCell.h"
#import "GodsEvaluListModel.h"
#import "GodsEvaluateListCell.h"
@interface GoddsEvaluateVC ()

@property (nonatomic, assign) NSInteger curpage;

@property (nonatomic, assign) NSString *pageCount;

@property (nonatomic, assign) NSString *totalCount;

@property (nonatomic, strong) NSMutableArray<GodsEvaluListModel *> *dataModels;
@end

@implementation GoddsEvaluateVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"商品评价";
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.plainTableView.backgroundColor = viewBgGrey;
    
    self.plainTableView.rowHeight = UITableViewAutomaticDimension;
    
    
    self.plainTableView.estimatedRowHeight = 500;
    
    [self.plainTableView registerNib:[UINib nibWithNibName:NSStringFromClass([GodsEvaluateListCell class]) bundle:nil] forCellReuseIdentifier:[GodsEvaluateListCell identifierStr]];
    
    
    [self.plainTableView registerNib:[UINib nibWithNibName:NSStringFromClass([EvalusteNothingCell class]) bundle:nil] forCellReuseIdentifier:[EvalusteNothingCell identifierStr]];

    
    MJWeakSelf
    self.plainTableView.mj_header = [XMGRefreshHeader headerWithRefreshingBlock:^{
        
        [weakSelf loadData];
        
    }];
    
    [weakSelf.plainTableView.mj_header beginRefreshing];
    
    
    
    
    
    // Do any additional setup after loading the view.
}





- (void)loadData{
    self.curpage = 1;
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"page"] = @"1";
    parameters[@"pagesize"] = @"20";
    parameters[@"goods_id"] = self.good_id;

    //    keyword
    
    //    parameters[@"uid"] = myViewModel.user.uid;
    
    MJWeakSelf;
    [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:GET_METHOD URLPath:@"good/getOrderEvaluateByOrder" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
            
//                                                MYJSONSTR
            
        [[responseObject[@"data"][@"data"] firstObject]  propertyCode];
            
            weakSelf.pageCount = responseObject[@"data"][@"page_count"];
            
            weakSelf.totalCount = responseObject[@"data"][@"total_count"];
            
            
            weakSelf.dataModels = [GodsEvaluListModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"data"]];
            

            
            if ([weakSelf.totalCount isEqualToString:@"0"]) {
                
                
                [weakSelf.plainTableView.mj_header endRefreshing];
                
                
                
                [weakSelf.plainTableView reloadData];
                
            } else {
                
                [weakSelf cachePictures:weakSelf.dataModels];
                
             
            }
            
          
            
        } else {
            
            [weakSelf.plainTableView.mj_header endRefreshing];
            
            [SVProgressHUD showInfoWithStatus:error.domain];
            
        }
    }];
    
    
    
}



- (void)loadMoreData{
    
    self.curpage ++;
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"pagesize"] = @"20";
    parameters[@"page"] = [NSString stringWithFormat:@"%ld",(long)self.curpage];
    parameters[@"goods_id"] = @"2";

    
    MJWeakSelf;
    [[NetworkTools shareNetworkTools] sendRequestWithHttpMethod:GET_METHOD URLPath:@"good/getOrderEvaluateByOrder" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
            
            NSMutableArray<GodsEvaluListModel *> *temp = [GodsEvaluListModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"data"]];
            
            
            
            [weakSelf.dataModels addObjectsFromArray:temp];
            
            
            [weakSelf cacheMorePictures:temp];
            
        
            
        }else{
            
            [weakSelf.plainTableView.mj_footer endRefreshing];
            
            [SVProgressHUD showInfoWithStatus:error.domain];
            
        }
        
    }];
    
    
    
}


- (void)cachePictures:(NSMutableArray<GodsEvaluListModel *> *)models{
    
    
    
    
    dispatch_group_t group = dispatch_group_create();
    
    
    for (GodsEvaluListModel *model in models) {
        if (model.image.count == 1) {
            dispatch_group_enter(group);
            [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:model.image.firstObject] options:kNilOptions progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                
                NSLog(@"111111111");
                
                dispatch_group_leave(group);
                
            }];
        }
    }
    
  
    
    MJWeakSelf;
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        
        
        
        weakSelf.plainTableView.mj_footer = [XMGRefreshFooter footerWithRefreshingBlock:^{
            
            [weakSelf loadMoreData];
            
        }];
        
        [weakSelf.plainTableView.mj_footer resetNoMoreData];
        
        
        if ([weakSelf.pageCount isEqualToString:@"1"]) {
            
            [weakSelf.plainTableView.mj_footer endRefreshingWithNoMoreData];
            
        }
        
        
        [weakSelf.plainTableView.mj_header endRefreshing];
        
        
        
        [weakSelf.plainTableView reloadData];
        
        
    });
    

    

    
    
    
}





- (void)cacheMorePictures:(NSMutableArray<GodsEvaluListModel *> *)models{
    
    
    
    
    dispatch_group_t group = dispatch_group_create();
    
    
    for (GodsEvaluListModel *model in models) {
        if (model.image.count == 1) {
            dispatch_group_enter(group);
            [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:model.image.firstObject] options:kNilOptions progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                
                NSLog(@"111111111");
                
                dispatch_group_leave(group);
                
            }];
        }
    }
    
    
    
    MJWeakSelf;
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        
        
        
        if (weakSelf.pageCount.integerValue == self.curpage) {
            
            [weakSelf.plainTableView.mj_footer endRefreshingWithNoMoreData];
            
        } else {
            
            [weakSelf.plainTableView.mj_footer endRefreshing];
            
            
        }
        
        
        [weakSelf.plainTableView reloadData];
        
        
    });
    
    
    
    
    
    
    
}


#pragma mark - tableviewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    if (self.dataModels == nil) {
        return  0;
    } else {
        return  1;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.dataModels == nil) {
        return  0;
    } else {
        
        if (self.dataModels.count == 0) {
            return 1;
        } else {
            return  self.dataModels.count;
        }
        
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.dataModels.count == 0) {
        
        
        EvalusteNothingCell *cell = [tableView dequeueReusableCellWithIdentifier:[EvalusteNothingCell identifierStr]];
        
        
        return cell;
        
        
    } else {
        
        
        GodsEvaluateListCell *cell = [tableView dequeueReusableCellWithIdentifier:[GodsEvaluateListCell identifierStr]];
        
        cell.model = self.dataModels[indexPath.row];
        
        
        return cell;
        
        
    }
    
    
    
}










- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
